#coding:utf-8
from Tkinter import *
import time,itertools,sys,time
reload(sys)   
sys.setdefaultencoding('utf8') 
from fake_useragent import UserAgent
ua = UserAgent()
import requests
from lxml import etree

class ZhiPin(object):
	"""docstring for ZhiPin"""
	headers = {
			"User-Agent":ua.random,
			"Accept":"*/*",
			"Accept-Encoding":"gzip, deflate",
			"Accept-Language":"zh-CN,zh;q=0.8",
			
			'Upgrade-Insecure-Requests':'1',
			"Connection":"keep-alive",
			}
	def __init__(self):
		#print "kaishi"
		self.root = Tk()
		#软件名称
		self.root.title('直聘网职位搜索神器--BY Jake')
		#设置软件长宽尺寸
		self.root.geometry('800x700+100+20')
		#定义三个容器区域
		self.fm1 = Frame(self.root,bg = '',width = 780,height = 60)
		self.fm2 = Frame(self.root,bg = '',width = 780,height = 280)
		self.fm3 = Frame(self.root,bg = '',width = 780,height = 240)

		#定义第一个容器区域内容
		# 创建一个Label,它的master为fm1
		self.label = Label(self.fm1,text = '检索关键字：(为了不引起显示异常，请勿调整显示框大小！)',font=("Purisa",16))
		self.label.grid(row = 0,column = 0)
		#self.label1.pack(side = LEFT)

		#创建输入框，输入检索关键字
		self.entry_keyword = Entry(self.fm1,borderwidth = 3,width = 25,font=("Purisa",20))
		self.entry_keyword.grid(row = 1,column = 0)


		#显示所定义的三个容器区域
		self.fm1.pack()
		self.fm2.pack()
		self.fm3.pack()

		#填充文本标题
		self.info_tag()
		#文本显示框初始化定义
		self.text_k()
		#按钮初始化定义
		self.button_all()

		self.root.mainloop()
	#文本显示框
	def text_k(self):
		#定义文本框，显示数据
		self.name = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.name.grid(row = 3,column = 1)
		self.data = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.data.grid(row = 3,column = 3)
		self.red = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.red.grid(row = 4,column = 1)
		self.place = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.place.grid(row = 4,column = 3)
		self.experience = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.experience.grid(row = 5,column = 1)
		self.degree = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.degree.grid(row = 5,column = 3)
		self.company_name = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.company_name.grid(row = 6,column = 1)
		self.belong_line = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.belong_line.grid(row = 6,column = 3)
		self.company_num = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.company_num.grid(row = 7,column = 1)
		self.tag_name = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.tag_name.grid(row = 7,column = 3)
		self.job_author = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.job_author.grid(row = 8,column = 1)
		self.job_author_station = Entry(self.fm2,borderwidth = 3,width = 20,font=("Purisa",18))
		self.job_author_station.grid(row = 8,column = 3)
		#TEXT长文本显示框-----显示招聘岗位的详情
		self.info_xq = Text(self.fm3,borderwidth = 5,height = 12,width = 50,font=("Purisa",17))
		self.info_xq.grid(row = 1,column = 1)
	#文本显示标签
	def info_tag(self):
		#显示得到的数据，填充到文本框
		self.label1 = Label(self.fm2,text = ' 岗位:',font=("Purisa",12),width = 14,height = 2)
		self.label1.grid(row = 3,column = 0)
		self.label2 = Label(self.fm2,text = '发布日期:',font=("Purisa",12),width = 14,height = 2)
		self.label2.grid(row = 3,column = 2)
		self.label3 = Label(self.fm2,text = ' 薪酬:',font=("Purisa",12),width = 14,height = 2)
		self.label3.grid(row = 4,column = 0)
		self.label4 = Label(self.fm2,text = '工作地点:',font=("Purisa",12),width = 14,height = 2)
		self.label4.grid(row = 4,column = 2)
		self.label5 = Label(self.fm2,text = '经验:',font=("Purisa",12),width = 14,height = 2)
		self.label5.grid(row = 5,column = 0)
		self.label6 = Label(self.fm2,text = '学历要求:',font=("Purisa",12),width = 14,height = 2)
		self.label6.grid(row = 5,column = 2)
		self.label7 = Label(self.fm2,text = '企业:',font=("Purisa",12),width = 14,height = 2)
		self.label7.grid(row = 6,column = 0)
		self.label8 = Label(self.fm2,text = '所属行业:',font=("Purisa",12),width = 14,height = 2)
		self.label8.grid(row = 6,column = 2)
		self.label10 = Label(self.fm2,text = '规模:',font=("Purisa",12),width = 14,height = 2)
		self.label10.grid(row = 7,column = 0)
		self.label8 = Label(self.fm2,text = '标签分类:',font=("Purisa",12),width = 14,height = 2)
		self.label8.grid(row = 7,column = 2)
		self.label9 = Label(self.fm2,text = '发布者:',font=("Purisa",12),width = 14,height = 2)
		self.label9.grid(row = 8,column = 0)
		self.label10 = Label(self.fm2,text = '发布者岗位:',font=("Purisa",12),width = 14,height = 2)
		self.label10.grid(row = 8,column = 2)
		self.label11 = Label(self.fm3,text = '详情介绍:',font=("Purisa",12),width = 14,height = 2)
		self.label11.grid(row = 0,column = 0)
	#按钮定义
	def button_all(self):
		#创建退出按钮，调用search函数获取数据
		self.quit_button = Button(self.fm1,text = "退出",bg = 'red',font=("Purisa",11),\
							width = 10,height = 2, command = self.fm1.quit)
		self.quit_button.grid(row = 0,column =2)	

		#创建搜索按钮，调用search函数获取数据
		self.search_button = Button(self.fm1,text = "搜 索",font=("Purisa",11),\
							width = 10,height = 2, command = self.search)
		self.search_button.grid(row = 1,column =2)
		#创建搜索上一页按钮，调用search_lastpage函数获取数据
		self.last_button = Button(self.fm1,text = "上一页",font=("Purisa",11),\
							width = 10,height = 2, command = self.search_lastpage)
		self.last_button.grid(row = 0,column =3)		
		#创建搜索下一页按钮，调用search_nextpage函数获取数据
		self.next_button = Button(self.fm1,text = "下一页",font=("Purisa",11),\
							width = 10,height = 2, command = self.search_nextpage)
		self.next_button.grid(row = 1,column =3)

		#创建搜索上一条按钮，调用search_lastpage函数获取数据
		self.last_button_b = Button(self.fm3,text = "上一条",font=("Purisa",11),\
							width = 10,height = 2, command = self.search_last)
		self.last_button_b.grid(row = 15,column =0)		
		#创建搜索下一条按钮，调用search_nextpage函数获取数据
		self.next_button_b = Button(self.fm3,text = "下一条",font=("Purisa",11),\
							width = 10,height = 2, command = self.search_next)
		self.next_button_b.grid(row = 15,column =1)
	#默认开始搜索获取第一页招聘数据
	def search(self):
		#初始化爬取页面页码
		self.i = int(1)
		#获取输入进来的搜索关键字
		self.keyword = self.entry_keyword.get()
		if len(self.keyword) != 0 :
			self.url = 'http://www.zhipin.com/c100010000/h_100010000/?query=' + \
					str(self.keyword) + '&page=' + str(self.i) + '&ka=page-' + str(self.i)	
			self.info = self.requests_html_info(self.url)
			#初始化显示数据序号
			self.num = int(0)
			#显示下一条数据之前清空文本框内容
			self.name.delete(0,END) 
			self.data.delete(0,END) 
			self.red.delete(0,END) 
			self.place.delete(0,END) 
			self.experience.delete(0,END) 
			self.degree.delete(0,END) 
			self.company_name.delete(0,END) 
			self.belong_line.delete(0,END) 
			self.company_num.delete(0,END) 
			self.tag_name.delete(0,END) 
			self.job_author.delete(0,END) 
			self.job_author_station.delete(0,END) 
			self.info_xq.delete('1.0',END)	
			#填充框自动写入获取的第一条数据
			#插入获取到的招聘信息到文本框
			self.name.insert(0,self.info[self.num][1])
			self.data.insert(0,self.info[self.num][2])
			self.red.insert(0,self.info[self.num][3])		
			self.place.insert(0,self.info[self.num][4])
			self.experience.insert(0,self.info[self.num][5])
			self.degree.insert(0,self.info[self.num][6])
			self.company_name.insert(0,self.info[self.num][7])
			self.belong_line.insert(0,self.info[self.num][8])
			self.company_num.insert(0,self.info[self.num][9])
			self.tag_name.insert(0,self.info[self.num][10])
			self.job_author.insert(0,self.info[self.num][11])
			self.job_author_station.insert(0,self.info[self.num][12])

			xiangqing = self.Get_xiangqing(self.info[self.num][0])
			self.info_xq.insert(INSERT,xiangqing)
		else :
			self.info_xq.delete('1.0',END)
			self.info_xq.insert(INSERT,"请输入搜索关键字！")
	#显示下一条信息
	def search_next(self):
		#显示下一条数据之前清空文本框内容
		self.name.delete(0,END) 
		self.data.delete(0,END) 
		self.red.delete(0,END) 
		self.place.delete(0,END) 
		self.experience.delete(0,END) 
		self.degree.delete(0,END) 
		self.company_name.delete(0,END) 
		self.belong_line.delete(0,END) 
		self.company_num.delete(0,END) 
		self.tag_name.delete(0,END) 
		self.job_author.delete(0,END) 
		self.job_author_station.delete(0,END) 
		self.info_xq.delete('1.0',END)	
		#数据序号num = num + 1
		self.num = self.num + 1
		if self.num < len(self.info)  and self.num >= 0:
			#插入获取到的招聘信息到文本框
			self.name.insert(0,self.info[self.num][1])
			self.data.insert(0,self.info[self.num][2])
			self.red.insert(0,self.info[self.num][3])		
			self.place.insert(0,self.info[self.num][4])
			self.experience.insert(0,self.info[self.num][5])
			self.degree.insert(0,self.info[self.num][6])
			self.company_name.insert(0,self.info[self.num][7])
			self.belong_line.insert(0,self.info[self.num][8])
			self.company_num.insert(0,self.info[self.num][9])
			self.tag_name.insert(0,self.info[self.num][10])
			self.job_author.insert(0,self.info[self.num][11])
			self.job_author_station.insert(0,self.info[self.num][12])
			xiangqing = self.Get_xiangqing(self.info[self.num][0])
			self.info_xq.insert(INSERT,xiangqing)			
		else:
			#当前页面数据显示完了，自动获取下一页数据
			self.info_xq.delete('1.0',END)
			self.info_xq.insert(INSERT,"没有下一条了！请获取下一页信息再继续！")
	#显示上一条信息
	def search_last(self):
		#显示下一条数据之前清空文本框内容
		self.name.delete(0,END) 
		self.data.delete(0,END) 
		self.red.delete(0,END) 
		self.place.delete(0,END) 
		self.experience.delete(0,END) 
		self.degree.delete(0,END) 
		self.company_name.delete(0,END) 
		self.belong_line.delete(0,END) 
		self.company_num.delete(0,END) 
		self.tag_name.delete(0,END) 
		self.job_author.delete(0,END) 
		self.job_author_station.delete(0,END) 
		self.info_xq.delete('1.0',END)	
		#数据序号num = num + 1
		self.num = self.num - 1
		if self.num >= 0 and self.num < len(self.info) :
			#插入获取到的招聘信息到文本框
			self.name.insert(0,self.info[self.num][1])
			self.data.insert(0,self.info[self.num][2])
			self.red.insert(0,self.info[self.num][3])		
			self.place.insert(0,self.info[self.num][4])
			self.experience.insert(0,self.info[self.num][5])
			self.degree.insert(0,self.info[self.num][6])
			self.company_name.insert(0,self.info[self.num][7])
			self.belong_line.insert(0,self.info[self.num][8])
			self.company_num.insert(0,self.info[self.num][9])
			self.tag_name.insert(0,self.info[self.num][10])
			self.job_author.insert(0,self.info[self.num][11])
			self.job_author_station.insert(0,self.info[self.num][12])
			xiangqing = self.Get_xiangqing(self.info[self.num][0])
			self.info_xq.insert(INSERT,xiangqing)	
		else :
			self.info_xq.insert(INSERT,"当前没有上一条消息了！")	
	#搜索下一页数据
	def search_nextpage(self):
		#初始化爬取页面页码 i = i + 1
		self.i = self.i + 1
		if self.i < 10 and self.i > 0 :
			#获取输入进来的搜索关键字
			self.keyword = self.entry_keyword.get()
			if len(str(self.keyword)) != 0 :
				self.url = 'http://www.zhipin.com/c100010000/h_100010000/?query=' + \
						str(self.keyword) + '&page=' + str(self.i) + '&ka=page-' + str(self.i)	
				self.info = self.requests_html_info(self.url)
				#初始化显示数据序号
				self.num = int(0)
				#显示下一条数据之前清空文本框内容
				self.name.delete(0,END) 
				self.data.delete(0,END) 
				self.red.delete(0,END) 
				self.place.delete(0,END) 
				self.experience.delete(0,END) 
				self.degree.delete(0,END) 
				self.company_name.delete(0,END) 
				self.belong_line.delete(0,END) 
				self.company_num.delete(0,END) 
				self.tag_name.delete(0,END) 
				self.job_author.delete(0,END) 
				self.job_author_station.delete(0,END) 
				self.info_xq.delete('1.0',END)	
				#填充框自动写入获取的第一条数据
				#插入获取到的招聘信息到文本框
				self.name.insert(0,self.info[self.num][1])
				self.data.insert(0,self.info[self.num][2])
				self.red.insert(0,self.info[self.num][3])		
				self.place.insert(0,self.info[self.num][4])
				self.experience.insert(0,self.info[self.num][5])
				self.degree.insert(0,self.info[self.num][6])
				self.company_name.insert(0,self.info[self.num][7])
				self.belong_line.insert(0,self.info[self.num][8])
				self.company_num.insert(0,self.info[self.num][9])
				self.tag_name.insert(0,self.info[self.num][10])
				self.job_author.insert(0,self.info[self.num][11])
				self.job_author_station.insert(0,self.info[self.num][12])
				xiangqing = self.Get_xiangqing(self.info[self.num][0])
				self.info_xq.insert(INSERT,xiangqing)				
			else:
				self.info_xq.delete('1.0',END)			
				self.info_xq.insert(INSERT,"没有下一页数据！当前请求页面超过设定值！！")

		else :
			self.info_xq.delete('1.0',END)			
			self.info_xq.insert(INSERT,"请重新输入搜索关键字！")
	#搜索上一页数据
	def search_lastpage(self):
		#初始化爬取页面页码 i = i + 1
		self.i = self.i - 1
		if self.i < 10 and self.i > 0 :
			#获取输入进来的搜索关键字
			self.keyword = self.entry_keyword.get()
			if len(self.keyword) != 0 :
				self.url = 'http://www.zhipin.com/c100010000/h_100010000/?query=' + \
						str(self.keyword) + '&page=' + str(self.i) + '&ka=page-' + str(self.i)	
				self.info = self.requests_html_info(self.url)
				#初始化显示数据序号
				self.num = int(0)
				#显示下一条数据之前清空文本框内容
				self.name.delete(0,END) 
				self.data.delete(0,END) 
				self.red.delete(0,END) 
				self.place.delete(0,END) 
				self.experience.delete(0,END) 
				self.degree.delete(0,END) 
				self.company_name.delete(0,END) 
				self.belong_line.delete(0,END) 
				self.company_num.delete(0,END) 
				self.tag_name.delete(0,END) 
				self.job_author.delete(0,END) 
				self.job_author_station.delete(0,END) 
				self.info_xq.delete('1.0',END)	
				#填充框自动写入获取的第一条数据
				#插入获取到的招聘信息到文本框
				self.name.insert(0,self.info[self.num][1])
				self.data.insert(0,self.info[self.num][2])
				self.red.insert(0,self.info[self.num][3])		
				self.place.insert(0,self.info[self.num][4])
				self.experience.insert(0,self.info[self.num][5])
				self.degree.insert(0,self.info[self.num][6])
				self.company_name.insert(0,self.info[self.num][7])
				self.belong_line.insert(0,self.info[self.num][8])
				self.company_num.insert(0,self.info[self.num][9])
				self.tag_name.insert(0,self.info[self.num][10])
				self.job_author.insert(0,self.info[self.num][11])
				self.job_author_station.insert(0,self.info[self.num][12])
				xiangqing = self.Get_xiangqing(self.info[self.num][0])
				self.info_xq.insert(INSERT,xiangqing)				
			else:
				self.info_xq.delete('1.0',END)			
				self.info_xq.insert(INSERT,"输入关键字不正确！请重新输入搜索关键字！")
		else :
			self.info_xq.delete('1.0',END)			
			self.info_xq.insert(INSERT,"没有上一页数据！当前请求页面超过设定值！！")
	#请求网页数据
	def requests_html_info(self,url):
		#请求页面数据
		html = requests.get(self.url,headers = self.headers)
		time.sleep(2)
		#print html.text
		self.job_list = []
		tree = etree.HTML(html.text)
		lis = tree.xpath('//*[@id="main"]/div[3]/div[2]/ul/li')
		for li in lis :
			#职位详情链接
			href = li.xpath('a/@href')
			if len(href) == 0 :
				href = "None"
			else :
				href = 'http://www.zhipin.com' + href[0]
			#职位名称
			name = li.xpath('a/div[1]/div[1]/h3/text()')
			if len(name) == 0 :
				name = "None"
			else :
				name = name[0]
			#发布日期
			data = li.xpath('a/div[3]/span/text()')
			if len(data) == 0 :
				data = "None"
			else :
				data = data[0]
			#薪酬
			red = li.xpath('a/div[1]/div[1]/h3/span/text()')
			if len(red) == 0 :
				red = "None"
			else :
				red = red[0]
			#工作地点
			place = li.xpath('a/div[1]/div[1]/p/text()[1]')
			if len(place) == 0 :
				place = "None"
			else :
				place = place[0]
			#工作经验
			experience = li.xpath('a/div[1]/div[1]/p/text()[2]')
			if len(experience) == 0 :
				experience = "None"
			else :
				experience = experience[0]
			#学历要求
			degree = li.xpath('a/div[1]/div[1]/p/text()[3]')
			if len(degree) == 0 :
				degree = "None"
			else :
				degree = degree[0]
			#招聘企业名称
			company_name = li.xpath('a/div[1]/div[2]/div/h3/text()')
			if len(company_name) == 0 :
				company_name = "None"
			else :
				company_name = company_name[0]
			#所属行业
			belong_line = li.xpath('a/div[1]/div[2]/div/p/text()[1]')
			if len(belong_line) == 0 :
				belong_line = "None"
			else :
				belong_line = belong_line[0]
			#公司规模人数
			company_num = li.xpath('a/div[1]/div[2]/div/p/text()[2]')
			if len(company_num) == 0 :
				company_num = "None"
			else :
				company_num = company_num[0]
			#标签分类
			tag_name = li.xpath('a/div[2]/span/text()')
			tagname = ["-*-".join(itertools.chain(tag_name))]
			if len(tagname) == 0 :
				tagname = "None"
			else :
				tagname = tagname[0]
			#发布者
			job_author = li.xpath('a/div[2]/div/p/text()[1]')
			if len(job_author) == 0 :
				job_author = "None"
			else :
				job_author = job_author[0]
			#发布者所在岗位 
			job_author_station = li.xpath('a/div[2]/div/p/text()[2]')
			if len(job_author_station) == 0 :
				job_author_station = "None"
			else :
				job_author_station = job_author_station[0]
			job =[href,name,data,red,place,experience,degree,company_name,\
					belong_line,company_num,tagname,job_author,job_author_station]
			self.job_list.append(job)
		return self.job_list
	#获取当前职位的详情说明
	def Get_xiangqing(self,url):
		time.sleep(0.1)
		html = requests.get(url,headers = self.headers)
		tree = etree.HTML(html.text)
		text = tree.xpath('//*[@id="main"]/div[3]/div/div[2]/div[3]/div[1]/div/text()')
		text = "".join(itertools.chain(text))
		line = "详情页链接：" + url + '\n' + text.replace(' ','')#.replace('','').replace(' ','')
		return line
zhipin = ZhiPin()